<?php
namespace Admin\Controller;
use Think\Controller;

class UserController extends CommonController {
	public function index(){
		$this->display();
	}
	/**
	 * 用户列表
	 */
	public function user_list(){
		$where = ' 1 ';
		if(I('search')){
			$where = $where.' and (nickname like "%'.I('search').'%" or phone like "%'.I('search').'%")';
		}
		$atime = strtotime(I('atime'));
		if($atime){$where = $where.' and rtime>='.$atime;}
		$aetime = strtotime(I('aetime'));
		if($aetime){$where = $where.' and rtime<='.$aetime;}
		if(I('sex')){$where = $where.' and sex='.I('sex');}
//		var_dump($where);die;
		$users = M('user');
		$count = $users->where($where)->count();
		$Page = new \Think\Page($count,10);
		$Page->parameter['search'] = I('search');
		$Page->parameter['atime'] = $atime;
		$Page->parameter['aetime'] = $aetime;
		$Page->parameter['sex'] = I('sex');
		$show = $Page->show();
		$user = $users->where($where)->order(' rtime desc ')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->atime=I('atime');$this->aetime=I('aetime');$this->sex=I('sex');
		$this->search = I('search');
		$this->page = $show;
		$this->user = $user;
		$this->display();
	}
	/**
	 * 导出用户数据
	 */
	public function out_user_lit(){
		$where = ' 1 ';
		if(I('search')){
			$where = $where.' and (nickname like "%'.I('search').'%" or phone like "%'.I('search').'%")';
		}
		$atime = strtotime(I('atime'));
		if($atime){$where = $where.' and rtime>='.$atime;}
		$aetime = strtotime(I('aetime'));
		if($aetime){$where = $where.' and rtime<='.$aetime;}
		if(I('sex')){$where = $where.' and sex='.I('sex');}
//		var_dump(I('atime').'-'.I('aetime').'-'.I('sex').'-'.I('search'));
//		var_dump($where);die;
		$user = M('user')->where($where)->order(' rtime desc ')->select();
//		var_dump($user);die;
		$excel_obj = new \Common\Api\Excel();
		$excel_data = array();	
		$excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'14','Bold'=>'1')));	
		$excel_data[0][] = array('styleid'=>'s_title','data'=>'姓名');
		$excel_data[0][] = array('styleid'=>'s_title','data'=>'手机');
		$excel_data[0][] = array('styleid'=>'s_title','data'=>'性别');
		$excel_data[0][] = array('styleid'=>'s_title','data'=>'称谓');
		$excel_data[0][] = array('styleid'=>'s_title','data'=>'地区');
		$excel_data[0][] = array('styleid'=>'s_title','data'=>'注册时间');
		$excel_data[0][] = array('styleid'=>'s_title','data'=>'最后登录时间');
		foreach($user as $v){
			$tmp = array();
			$tmp[] = array('data'=>$v['nickname']);
			$tmp[] = array('data'=>$v['phone']);
			if($v['sex']==1){$sex='男';}
			if($v['sex']==2){$sex='女';}
			if($v['sex']==3){$sex='保密';}
			$tmp[] = array('data'=>$sex);
			$tmp[] = array('data'=>$v['titlename']);
			$tmp[] = array('data'=>$v['area']);
			$tmp[] = array('data'=>date('Y-m-d',$v['rtime']));
			$tmp[] = array('data'=>date('Y-m-d',$v['ltime']));
			$excel_data[] = $tmp;
		}
		$excel_data = $excel_obj->charset($excel_data,'utf-8');
		$excel_obj->addArray($excel_data);
		//控制列的长度
		$column = array(array('column'=>2,'width'=>80),array('column'=>6,'width'=>100),array('column'=>7,'width'=>100));
		$excel_obj->addWorksheet($excel_obj->charset('活动详情','utf-8'),$column);
		$excel_obj->generateXML($excel_obj->charset('用户数据','utf-8'));
	}
	
	
	/**
	 * 用户详情
	 */
	 public function user_info(){
	 	$user = M('user')->where(array('id'=>I('id')))->find();
		$this->user = $user;
		$this->display();
	 }
	/**
	 * 系统用户列表
	 */
	 public function sysuser_list(){
	 	$sysusers = M('sysuser');
		$count = $sysusers->count();
		$Page = new \Think\Page($count,10);
		$sysuser = $sysusers->order(' rtime desc ')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->page = $Page->show();
		$this->sysuser = $sysuser;
		$this->display();
	 }
	
	  
	  
	/**
	 * 用户默认头像列表
	 */
	 public function userimg_list(){
	 	$userimgs = M('user_autoimg');
		$count = $userimgs->count();
		$Page = new \Think\Page($count,10);
		$userimg = $userimgs->order(' id desc ')->limit($Page->firstRow.','.$Page->listRows)->select();
	 	$this->page = $Page->show();
		$this->userimg = $userimg;
		$this->display();
	 }
	 
	 public function userimg_add(){
	 	$this->display();
	 }
	 /**
	  * 增加或修改用户头像
	  */
	 public function userimg_save(){
		if(IS_POST){
			$upload = get_upload_img(C('USERAUTOIMG'));
			$imgurl = $upload['imgurl'];
			$savename = $imgurl['savename'];
			if($imgurl){//有上传图片
				$x = I('dataX');
				$y = I('dataY');
				$w = I('dataWidth');
				$h = I('dataHeight');
				$pre_imgurl = C('USERAUTOIMG').$imgurl['savepath'].'/'.$imgurl['savename'];//未剪切的图片位置
				$savepath = C('USERAUTOIMG');
				get_and_cut_upload_img($pre_imgurl,$savepath,$savename,$x,$y,$w,$h);//剪切图片并保存剪切图片
				$_POST['imgurl'] = C('TEAL_USERAUTOIMG').$savename;//保存图片绝对路径
			}
			if($_POST['id']){
					if(M('user_autoimg')->save($_POST)){
						$this->redirect('userimg_list');
					}else{
					$this->error('您根本就没修改此乐器！');
				}
				$this->redirect('instru_list');
			}else{
				if(M('user_autoimg')->add($_POST)){
					$this->redirect('userimg_list');
				}else{
					$this->error('添加失败，请联系程序猿！');
				}
			}
		}
	}
	/**
	 * 用户默认头像信息
	 */
	 public function userimg_info() {
    	$id = I('id');
		$userimgs = M('user_autoimg');
    	if($id){
    		$userimg = $userimgs->find($id);
			$this->userimg = $userimg;
        	$this->display();
		}
    }
	
	 
	 /**
	  * 删除用户头像
	  */
	 public function userimg_delete(){
	 	$id = I('id');
		if($id){
			if(M('user_autoimg')->delete($id)){
				$this->success('删除成功');
			};
		}
	 }
	  
}